<template>
  <DetailsFormBox :title="page.title" :type="route.query.type" :form="data.form" :btns="[
    {
      title: '重置',
      icon: '',
      type: '',
      hidden: !isEdit,
      click: getForm,
    },
    {
      title: '保存',
      icon: '',
      type: 'primary',
      hidden: !isEdit,
      click: (form: any) => {
        saveBtn(form, false)
      },
    },
  ]" :formInfo="[
    {
      label: '创建人员',
      value: 'createByName',
    },
    {
      label: '创建时间',
      value: 'createDate',
    },
  ]" :loading="data.loading" :loadingMsg="data.loadingMsg">
    <template #default="{ form }">
      <div class="p-12">
        <CellBox title="基本信息" isTop>
          <FlexBox>
            <ElFormItemBox label="单据单号" prop="saleOrderStatementNo">
              <ElInputBox :data="form" dataName="saleOrderStatementNo" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="客户名称" prop="customerCode" isRules>
              <ElSelectBox :data="form" dataName="customerCode" placeholder="请选择" :options="options.customer"
                :disabled="!isEdit" @changeBtn="selectCustomer" />
            </ElFormItemBox>
            <ElFormItemBox label="单据备注" prop="remark" class="wAuto">
              <ElInputBox :data="form" dataName="remark" type="textarea" :disabled="!isEdit" />
            </ElFormItemBox>
          </FlexBox>
        </CellBox>
        <template v-if="form.customerCode">
          <DetailsBox :data="form" :isEdit="isEdit" />
          <DetailsBox1 :data="form" :isEdit="isEdit" />
        </template>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { sendMsg } from '@/utils/crossTagMag.ts';
import DetailsBox from './DetailsBox.vue'; // 发货明细
import DetailsBox1 from './DetailsBox1.vue'; // 发货明细
import { useListComponent } from '../pageTs.ts'; // 引入逻辑
const { dataInfo, options } = useListComponent();
import page from '../page.ts'; // 引入逻辑
import '../pageCss.scss'; // 引入样式
const { proxy } = getCurrentInstance() as any;
const data: any = reactive({
  loading: false,
  loadingMsg: '',
  mainPage: 1,
  form: {
    saleOrderStatementCode: '', // 唯一code
    customerCode: '', // 	客户编码
    remark: '', // 备注说明
    saleOrderStatementExpressDetails: [],// 快递明细
    saleOrderStatementDeliveryDetails: [],// 发货明细-成品明细
    saleOrderStatementOtherDetails: [],// 其它明细
  },
});
const isEdit = ref(true)
//选择客户
const selectCustomer = () => {
  const _form = data.form
  _form.saleOrderStatementExpressDetails = []
  _form.saleOrderStatementDeliveryDetails = []
  _form.saleOrderStatementOtherDetails = []
}
import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
//获取数据
const getForm = async () => {
  const _query: any = route.query
  setLoading()
  //是否新建
  if (!!!_query.code) {
    data.loading = false
    return
  }
  proxy.$api.OrderBusniess.GetMatchSaleOrderStatementDetail({
    code: _query.code,
  })
    .then((res: any) => {
      proxy.$message('获取成功')
      isEdit.value = ['1', '2'].includes(_query.type)
      data.form = res
      data.loading = false
    })
    .catch(() => {
      data.loading = false
    });
};
const saveBtn = (form: any, isConfirm: boolean = false, isPartial?: number) => {
  const _form: any = {
    saleOrderStatementCode: route.query.type === '1' ? "" : form.saleOrderStatementCode, // 成品报废单唯一code
    customerCode: form.customerCode, // 成品报废单唯一code
    remarks: form.remarks, // 备注说明
    //快递明细
    saleOrderStatementExpressDetails: form.saleOrderStatementExpressDetails,
    //发货明细-成品明细
    saleOrderStatementDeliveryDetails: form.saleOrderStatementDeliveryDetails.map((el: any) => (
      {
        saleOrderStatementDeliveryDetailCode: el.saleOrderStatementDeliveryDetailCode,
        deliveryMxCode: el.deliveryMxCode, // 发货明细编码-不可重复
        price: el.price, //单价
        number: el.number, //	数量
        amount: el.amount, //金额 
        remark: el.remark, //备注 
        courierFee: el.courierFee, //快递费用 
        totalAmount: el.totalAmount, //费用总额 
        statementNumber: el.statementNumber, //对账数量
        courierFeeType: el.courierFeeType, //快递费用归属-默认是客户设置的类型：0-客户承担；1-公司承担；2-各自一半，默认0
        customerCourierFee: el.customerCourierFee, //客户快递费用
        receiverName: el.receiverName, //收货人
        receiveAddressAll: el.receiveAddressAll, //收货地址
      }
    )),
    //其它明细
    saleOrderStatementOtherDetails: form.saleOrderStatementOtherDetails.map((el: any) => (
      {
        saleOrderStatementOtherDetailCode: el.saleOrderStatementOtherDetailCode, //唯一编码
        saleOrderCode: el.saleOrderCode, //订单编码
        saleOrderFeeDetailCode: el.saleOrderFeeDetailCode, //订单其它费用明细Code-不可重复
        type: el.type, //费用类型名称
        price: el.price, //单价
        number: el.number, //数量
        amount: el.amount, //金额
        remark: el.remark, //备注
      }
    )),
  }
  // console.log(form);
  // return
  proxy.$api.OrderBusniess.AutoSaveSaleOrderStatement(_form)
    .then((res: any) => {
      _sun()
    })
    .catch(() => { });
  function _sun() {
    closeBtn('保存成功')
  }
};
const closeBtn = (message?: string) => {
  if (message) {
    proxy.$message(message)
  }
  sendMsg('save', true);
  // getForm()
  window.close();
};
const setLoading = (title: string = '正在获取数据,请稍等...') => {
  if (!data.loading) {
    data.loading = true;
    data.loadingMsg = title;
  }
};
import { getCustomer } from '@/utils/optionsArr.ts';
onMounted(() => {
  getForm();//客户
  getCustomer(options, 'customer');
});
</script>
